package com.tianxin.workbao.workBao.dataProvide;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.tianxin.workbao.workBao.bean.Appupdate;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.common.URL;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.okhttp.CommonOkHttpClient;
import com.tianxin.workbao.workBao.okhttp.exception.OkHttpException;
import com.tianxin.workbao.workBao.okhttp.listener.DisposeDataHandle;
import com.tianxin.workbao.workBao.okhttp.listener.DisposeDataListener;
import com.tianxin.workbao.workBao.okhttp.request.CommonRequest;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by hacker on 2017/7/25.
 */

public class ApkProvide extends BaseDataProvide {

    private Appupdate appupdate;
    private UpdateListViewListener mUpdateListViewListener;

    public ApkProvide(Context context, UpdateListViewListener listener) {
        super(context);
        mUpdateListViewListener = listener;
    }


    @Override
    public DisposeDataHandle getDisposeDataHandle() {
        disposeDataHandle = new DisposeDataHandle(getDisposeDataListener());
        return disposeDataHandle;
    }

    @Override
    public DisposeDataListener getDisposeDataListener() {
        disposeDataListener = new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                mUpdateListViewListener.hideProgress();
                try {
                    JSONObject object = new JSONObject(responseObj.toString());
                    int status = object.getInt(Constant.RESPONSE_STATUS);
                    String information = object.getString(Constant.RESPONSE_INFO);
                    if (status > 0) {
                        String dataJson = object.getString(Constant.RESPONSE_DATA);
                        if (dataJson != null || dataJson.length() > 2) {
                            try {
                                appupdate = JSON.parseObject(dataJson,Appupdate.class);
                                refreshUI();
                            } catch (Exception e) {
                                mUpdateListViewListener.showMessage("app update To Bean Error :类型转换出错" + e.getMessage());
                            }
                        } else {
                            mUpdateListViewListener.showMessage(information);
                        }
                    } else {
                        mUpdateListViewListener.showMessage(information);
                    }
                } catch (JSONException e) {
                    showRequestErrorMessage(mUpdateListViewListener, responseObj.toString());
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Object reasonObj) {
                mUpdateListViewListener.hideProgress();
                if (reasonObj instanceof OkHttpException) {
                    OkHttpException exception = (OkHttpException) reasonObj;
                    mUpdateListViewListener.showMessage(exception.getEcode() + ":" + exception.getEmsg().toString());
                } else {
                    showRequestErrorMessage(mUpdateListViewListener, reasonObj.toString());
                }
            }
        };
        return disposeDataListener;
    }


    /**
     * update the output bill  data  from server
     */
    public void getNew() {
        mUpdateListViewListener.showProgress();
        CommonOkHttpClient.post(CommonRequest.createPostRequest(URL.getNewApkUrl(), null), getDisposeDataHandle());
    }

    @Override
    public void refreshUI() {
        mUpdateListViewListener.onRefreshData(appupdate);
    }

}
